﻿using SuperLottery.AdminPortal.Models;
using SuperLottery.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SuperLottery.AdminPortal.Controllers
{
	public class XCrawler
	{
		public DrawModel Crawl(string issueNumber)
		{
			DrawModel model = new DrawModel();
			model.IssueNumber = issueNumber;
	
			string detailUrl = "http://www.chinafcw.com.cn/ulWinInfo.do?issueNumber={0}&method=detail";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			string drawDatePattern = @"<td width=""13%"" height=""36"" class=""leftfont"">开奖日期：</td>\s*<td width=""87%"" class=""leftfont"">(\d{4}-\d{1,2}-\d{1,2})";
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.DrawDate = drawDate;

			string numbersPattern = @"<td height=""36"" class=""leftfont"">红球号码：</td>[\s]*<td class=""kj_img""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""></td>";
			match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			string numbers = match.Groups.OfType<Group>().Skip(1).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			model.Numbers = numbers;

			string bonusNumbersPattern = @"<td height=""36"" class=""leftfont"">蓝球号码：</td>\s*<td class=""kj_img""><img src=""/images/num/b(\d{2}).jpg"" width=""38"" height=""28""></td>";
			match = Regex.Match(htmlSource, bonusNumbersPattern, RegexOptions.IgnoreCase);
			string bonusNumbers = match.Groups[1].Value;
			model.BonusNumbers = bonusNumbers;

			string amountPattern = @"本期投注金额为\s*(?<SalesAmount>\d*)\s*<br>\W*奖池资金累计为\s*(?<CumulativeAmount>\d*)\s*元";
			match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
			string strSalesAmount = match.Groups["SalesAmount"].Value;
			string strCumulativeAmount = match.Groups["CumulativeAmount"].Value;
			long salesAmount, cumulativeAmount;
			Int64.TryParse(strSalesAmount, out salesAmount);
			model.SalesAmount = salesAmount;
			Int64.TryParse(strCumulativeAmount, out cumulativeAmount);
			model.CumulativeAmount = cumulativeAmount;

			string winningDetailPattern = @"(?<=>)(?<Award>\w{2,}奖)(?=<\/td>)[\s\S]*?(?<=>)(?<NumberOfWinners>\d*?)(?=<\/td>)[\s\S]*?(?<=>)(?<MoneyPerWinner>\d*?)(?=<\/td>)";
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups["Award"].Value;
				string strNumberOfWinners = matchItem.Groups["NumberOfWinners"].Value;
				string strMoneyPerWinner = matchItem.Groups["MoneyPerWinner"].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				if (detail.Award == "快乐星期天特别奖" && detail.NumberOfWinners == 0)
				{
					continue;
				}

				details.Add(detail);
			}
			model.SetWinningDetails(details);
			return model;
		}

		public DrawModel CrawlQiLeCai(string issueNumber)
		{
			DrawModel model = new DrawModel();
			model.IssueNumber = issueNumber;

			string detailUrl = "http://www.chinafcw.com.cn/qlcWinInfo.do?issueNumber={0}&method=detail";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			string drawDatePattern = @"<td width=""13%"" height=""36"" class=""leftfont"">开奖日期：</td>\s*<td width=""87%"" class=""leftfont"">(\d{4}-\d{1,2}-\d{1,2})";
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.DrawDate = drawDate;

			string numbersPattern = @"/images/num/(\d{2})\.jpg";
			var matches = Regex.Matches(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			string numbers = matches.OfType<Match>().Select(pMatch => pMatch.Groups[1].Value).Aggregate((a, b) => a + "," + b);
			model.Numbers = numbers;

			string bonusNumbersPattern = @"/images/num/b(\d{2})\.jpg";
			match = Regex.Match(htmlSource, bonusNumbersPattern, RegexOptions.IgnoreCase);
			string bonusNumbers = match.Groups[1].Value;
			model.BonusNumbers = bonusNumbers;

			string amountPattern = @"本期投注金额为\s*(?<SalesAmount>\d*)\s*<br>\W*奖池资金累计为\s*(?<CumulativeAmount>\d*)\s*元";
			match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
			string strSalesAmount = match.Groups["SalesAmount"].Value;
			string strCumulativeAmount = match.Groups["CumulativeAmount"].Value;
			long salesAmount, cumulativeAmount;
			Int64.TryParse(strSalesAmount, out salesAmount);
			model.SalesAmount = salesAmount;
			Int64.TryParse(strCumulativeAmount, out cumulativeAmount);
			model.CumulativeAmount = cumulativeAmount;

			string winningDetailPattern = @"(?<=>)(?<Award>\w{2,}奖)(?=<\/td>)[\s\S]*?(?<=>)(?<NumberOfWinners>\d*?)(?=<\/td>)[\s\S]*?(?<=>)(?<MoneyPerWinner>\d*?)(?=<\/td>)";
			matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups["Award"].Value;
				string strNumberOfWinners = matchItem.Groups["NumberOfWinners"].Value;
				string strMoneyPerWinner = matchItem.Groups["MoneyPerWinner"].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				details.Add(detail);
			}
			model.SetWinningDetails(details);
			return model;
		}
	}
}